#include "dbusServer.hpp"

dbusServer::dbusServer()
{
}

void dbusServer::Boot()
{
  initMut.lock();
  workThread=std::thread([this](){
    connection=sdbus::createSessionBusConnection("org.HBTY.WorkDataManager");
    auto connObject=sdbus::createObject(*connection,"/org/HBTY/WorkDataManager");
    connObject->registerMethod("GetTaskNameList").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetTaskNameList);
    connObject->registerMethod("GetTaskIDList").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetTaskIDList);
    connObject->registerMethod("CreateTask").onInterface("org.HBTY.WorkDataManager").implementedAs(funcCreateTask);
    connObject->registerMethod("DeleteTask").onInterface("org.HBTY.WorkDataManager").implementedAs(funcDeleteTask);
    connObject->registerMethod("GetRecordInf").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetRecordInf);
    connObject->registerMethod("LoadTaskInf").onInterface("org.HBTY.WorkDataManager").implementedAs(funcLoadTaskInf);
    connObject->registerMethod("CreateRecord").onInterface("org.HBTY.WorkDataManager").implementedAs(funcCreateRecord);
    connObject->registerMethod("FinishRecord").onInterface("org.HBTY.WorkDataManager").implementedAs(funcFinishRecord);
    connObject->registerMethod("GetRecordPath").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetRecordPath);
    connObject->registerMethod("CheckStudentExists").onInterface("org.HBTY.WorkDataManager").implementedAs(funcCheckStudentExists);
    connObject->registerMethod("GetStudentInfo").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetStudentInf);
    connObject->registerMethod("GetRecordFinishHandleList").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetRecordFinishHandleList);
    connObject->registerMethod("GetRecordFinishHandleListWithTask").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetRecordFinishHandleListWithTask);
    connObject->registerMethod("GetTaskInf").onInterface("org.HBTY.WorkDataManager").implementedAs(funcGetTaskInf);
    connObject->registerMethod("OutputData").onInterface("org.HBTY.WorkDataManager").implementedAs(funcOutputData);
    connObject->finishRegistration();
    initMut.unlock();
    connection->enterEventLoop();
  });
  initMut.lock();
}

dbusServer::~dbusServer()
{
  if(workThread.joinable())
  {
    connection->leaveEventLoop();
    workThread.join();
  }
}

void dbusServer::ThrowError(const char* value)
{
  throw sdbus::Error("org.HBTY.WorkDataManager",value);
}

